package com.zz.inventory.controller;

import java.util.List;
import java.io.IOException;

import com.ruoyi.common.core.exception.ServiceException;
import com.zz.inventory.config.SchoolRedisService;
import com.zz.inventory.domain.ServiceResult;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.Classes;
import com.zz.inventory.service.IClassesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 班级管理Controller
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@RestController
@Slf4j
@RequestMapping("/classes")
public class ClassesController extends BaseController
{
    @Autowired
    private IClassesService classesService;
    @Autowired
    private SchoolRedisService schoolRedisService;

    /**
     * 查询班级管理列表
     */
    @RequiresPermissions("schoolmanage:classes:list")
    @GetMapping("/list")
    public TableDataInfo list(Classes classes)
    {
        startPage();
        List<Classes> list = classesService.selectClassesList(classes);
        return getDataTable(list);
    }

    /**
     * 导出班级管理列表
     */
    @RequiresPermissions("schoolmanage:classes:export")
    @Log(title = "班级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classes classes)
    {
        List<Classes> list = classesService.selectClassesList(classes);
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        util.exportExcel(response, list, "班级管理数据");
    }

    /**
     * 获取班级管理详细信息
     */
    @RequiresPermissions("schoolmanage:classes:query")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId) {
        // 先从缓存获取
        Classes cachedClass = schoolRedisService.getCachedClass(classId);
        if (cachedClass != null) {
            return success(cachedClass);
        }

        // 缓存没有则从数据库获取
        Classes classes = classesService.selectClassesByClassId(classId);
        if (classes != null) {
            schoolRedisService.cacheClass(classes);
        }
        return success(classes);
    }

    /**
     * 新增班级管理
     */
    @RequiresPermissions("schoolmanage:classes:add")
    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Classes classes) {
        try {
            int result = classesService.insertClasses(classes);
            if (result > 0){
                schoolRedisService.cacheClass(classes);
                return success(classes);
            }
            return error("新增失败");
        } catch (ServiceException e) {
            return error(e.getMessage()); // 返回业务校验的友好提示
        } catch (Exception e) {
            log.error("新增班级异常", e);
            return error("系统错误，请稍后再试");
        }
    }

    /**
     * 修改班级管理
     */
    @RequiresPermissions("schoolmanage:classes:edit")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Classes classes)
    {
        int result = classesService.updateClasses(classes);
        if (result > 0){
            // 更新成功存入redis
            schoolRedisService.cacheClass(classes);
        }
        return toAjax(classesService.updateClasses(classes));
    }

    /**
     * 删除班级管理
     */
    @RequiresPermissions("schoolmanage:classes:remove")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classIds}")
    public ServiceResult<Integer> remove(@PathVariable Long[] classIds) {
        ServiceResult<Integer> result = classesService.deleteClassesWithStudentCheck(classIds);
        if (result.isSuccess()){
            // 删除成功后清楚缓存
            for (Long classId : classIds){
                schoolRedisService.removeCachedClass(classId);
            }
        }
        return result;
    }

}
